2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex2_slaveTXMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx EUSCI_B I2C - Slave transfer multiple bytes
34  *
35  * Description: In this example, the device is configured as an I2C slave.
36  * When the slave has data available it pulls the IRQ line (P1.0) down to
37  * tell the master data is ready. The master interrupts on the GPIO transition
38  * and starts an I2C read. The first byte of the slave's packet is the length
39  * of the packet, including the length byte. The master then continues to
40  * read bytes until all data from the packet has been consumed. All data
41  * processing is done in the interrupt service routines and the device stays
42  * in LPM0. Run the corresponding master example before starting this example.
43  *
44  *
45  * /|\ /|\
46  * MSP430i2041 10k 10k MSP430i2041
47  * slave | | master
48  * ----------------- | | -----------------
49  * | P1.6/UCB0SCL|<-|----+->|P1.6/UCB0SCL |
50  * | | | | |
51  * | | | | |
52  * | P1.7/UCB0SDA|<-+------>|P1.7/UCB0SDA |
53  * | | | |
54  * | P1.0|--------->|P1.0 |
55  * | | | |
56  *
57  * Author: Zack Lalanne
58  ******************************************************************************/
59 
60 #include "driverlib.h"
61 
62 #define SLAVE_ADDRESS 0x48
63 #define NUM_OF_TX_BYTES 4
64 
66  0xBE,
67  0xEF,
68  0xA5};
69 
70 static volatile uint16_t TXDataIndex = 0;
71 
72 int main(void) {
73 
74  EUSCI_B_I2C_initSlaveParam i2cConfig = {
75  SLAVE_ADDRESS, // Slave Address
76  EUSCI_B_I2C_OWN_ADDRESS_OFFSET0,
77  EUSCI_B_I2C_OWN_ADDRESS_ENABLE
78  };
79 
80  WDT_hold(WDT_BASE);
81 
82  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
83  CS_setupDCO(CS_INTERNAL_RESISTOR);
84 
85  // Setting P1.6 and P1.7 as I2C pins
86  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
87  GPIO_PIN6 | GPIO_PIN7,
88  GPIO_PRIMARY_MODULE_FUNCTION);
89 
90  // Using P1.0 as slave interrupt pin
91  GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
92  GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
93 
94  // Setting up I2C communication as slave
95  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &i2cConfig);
96 
97  // Set master in transmit mode
98  EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
99 
100  // Enable the module for operation
101  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
102 
103  while(1) {
104 
105  // Reset the TX Buffer
106  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
107  EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
108 
109  // Interrupt when TX Buffer is empty or stop from master
110  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
111  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 |
112  EUSCI_B_I2C_STOP_INTERRUPT);
113 
114  // Tell master that data is ready
115  GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
116 
117  __bis_SR_register(LPM0_bits | GIE);
118  }
119 }
120 
121 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
122 #pragma vector=USCI_B0_VECTOR
123 __interrupt
124 #elif defined(__GNUC__)
125 __attribute__((interrupt(USCI_B0_VECTOR)))
126 #endif
127 void USCIB0_ISR(void) {
128  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG)) {
129  case USCI_NONE: break;
130  case USCI_I2C_UCALIFG: break;
131  case USCI_I2C_UCNACKIFG: break;
132  case USCI_I2C_UCSTTIFG: break;
133  case USCI_I2C_UCSTPIFG:
134 
135  // No more data, set IRQ high
136  GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
137  TXDataIndex = 0;
138 
139  // Exit LPM to start next transaction
140  __bic_SR_register_on_exit(LPM0_bits | GIE);
141  break;
142  case USCI_I2C_UCRXIFG3: break;
143  case USCI_I2C_UCTXIFG3: break;
144  case USCI_I2C_UCRXIFG2: break;
145  case USCI_I2C_UCTXIFG2: break;
146  case USCI_I2C_UCRXIFG1: break;
147  case USCI_I2C_UCTXIFG1: break;
148  case USCI_I2C_UCRXIFG0: break;
149  case USCI_I2C_UCTXIFG0:
150  EUSCI_B_I2C_slavePutData(EUSCI_B0_BASE, TXData[TXDataIndex++]);
151 
152  // Disable TX if all data is sent
153  if(TXDataIndex == NUM_OF_TX_BYTES) {
154  EUSCI_B_I2C_disableInterrupt(EUSCI_B0_BASE,
155  EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
156  }
157  break;
158  case USCI_I2C_UCBCNTIFG: break;
159  case USCI_I2C_UCCLTOIFG: break;
160  case USCI_I2C_UCBIT9IFG: break;
161  default: break;
162  }
163 }
const uint8_t TXData[NUM_OF_TX_BYTES]
void USCIB0_ISR(void)
#define NUM_OF_TX_BYTES
#define SLAVE_ADDRESS
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)